<?php

namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Product
 *
 * @ORM\Table(name="product")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ProductRepository")
 */
class Product
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="text")
     */
    private $description;

    /**
     * @var string
     *
     * @ORM\Column(name="customer_name", type="string", length=255)
     */
    private $customerName;

    /**
     * @var string
     *
     * @ORM\Column(name="price", type="decimal", precision=10, scale=2)
     */
    private $price;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="add_time", type="datetime")
     */
    private $addTime;

    /**
     * @ORM\ManyToMany(targetEntity="Material", mappedBy="products")
     * @ORM\JoinTable(name="products_materials",
     *  joinColumns={
     *      @ORM\JoinColumn(name="product_id", referencedColumnName="id")
     *  },
     *  inverseJoinColumns={
     *      @ORM\JoinColumn(name="material_id", referencedColumnName="id")
     *  }
     * )
     */
    private $materials;

    public function addMaterial(Material $material)
    {
        if ($this->materials->contains($material)) {
            return;
        }
        $this->materials->add($material);
        $material->addProduct($this);
    }

    public function removeMaterial(Material $material)
    {
        if (!$this->materials->contains($material)) {
            return;
        }
        $this->materials->removeElement($material);
        $material->removeProduct($this);
    }

    /**
     * That means this product is removed if the value is greater than 0.
     * @var int
     * @ORM\Column(name="remove", type="smallint", options={"default"=0})
     */
    private $remove;

    /**
     * @return int
     */
    public function getRemove(): int
    {
        return $this->remove;
    }

    /**
     * @param int $remove
     */
    public function setRemove(int $remove)
    {
        $this->remove = $remove;
    }

    /**
     * @return ArrayCollection
     */
    public function getMaterials()
    {
        return $this->materials;
    }

    /**
     * @param mixed $materials
     */
    public function setMaterials($materials)
    {
        $this->materials = $materials;
    }

    public function __construct()
    {
        $this->materials = new ArrayCollection();
    }

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Product
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set description
     *
     * @param string $description
     *
     * @return Product
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Set customerName
     *
     * @param string $customerName
     *
     * @return Product
     */
    public function setCustomerName($customerName)
    {
        $this->customerName = $customerName;

        return $this;
    }

    /**
     * Get customerName
     *
     * @return string
     */
    public function getCustomerName()
    {
        return $this->customerName;
    }

    /**
     * Set price
     *
     * @param string $price
     *
     * @return Product
     */
    public function setPrice($price)
    {
        $this->price = $price;

        return $this;
    }

    /**
     * Get price
     *
     * @return string
     */
    public function getPrice()
    {
        return $this->price;
    }

    /**
     * Set addTime
     *
     * @param \DateTime $addTime
     *
     * @return Product
     */
    public function setAddTime($addTime)
    {
        $this->addTime = $addTime;

        return $this;
    }

    /**
     * Get addTime
     *
     * @return \DateTime
     */
    public function getAddTime()
    {
        return $this->addTime;
    }

    public function __toString()
    {
        return json_encode([

            'id' => $this->getId(),
            'name' => $this->getName(),
            'description' => $this->getDescription(),
            'price' => $this->getPrice(),
            'customer_name' => $this->getCustomerName(),
            'add_time' => $this->getAddTime()->format('Y-m-d H:i:s'),
            'remove' => $this->getRemove(),
        ]);
    }
}

